//
//  UIHelper.m
//  YImage
//
//  Created by bing on 16/3/11.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "UIHelper.h"
#import <MBProgressHUD.h>
#import <QuartzCore/QuartzCore.h>

@implementation UIHelper

+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

+ (UIColor *)getColorFromRGB:(int)rgbValue
{
    return [self getColorFromRGB:rgbValue alpha:1];
}

+ (UIColor *)getColorFromRGB:(int)rgbValue alpha:(CGFloat)alpha
{
    float red   = ((float)((rgbValue & 0xFF0000) >> 16))/255.0;
    float green = ((float)((rgbValue & 0xFF00) >> 8))/255.0;
    float blue  = ((float)(rgbValue & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (void)initNavicationViewCommonUI:(UIViewController *)viewController
{
    UINavigationBar *navigationBar = viewController.navigationController.navigationBar;
    [navigationBar setBackgroundImage:nil forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    navigationBar.translucent = YES;
    
    navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor], NSForegroundColorAttributeName,
                                         [UIFont systemFontOfSize:16.0f], NSFontAttributeName,
                                         nil];
}

+ (void)initNavicationViewTransparentUI:(UIViewController *)viewController
{
    UINavigationBar *bar = viewController.navigationController.navigationBar;
    [bar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [bar setShadowImage:[UIImage new]];
    bar.translucent = YES;
    
    bar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                               [UIColor grayColor], NSForegroundColorAttributeName,
                               [UIFont systemFontOfSize:16.0f], NSFontAttributeName,
                               nil];
}


#pragma mark - HUD

+ (void)showTextHUD:(NSString *)prompt
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = prompt;
        [hud hide:YES afterDelay:2.0];
    });
}

+ (void)showLoadingHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [MBProgressHUD showHUDAddedTo:window animated:YES];
    });
}

+ (void)hideHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [MBProgressHUD hideHUDForView:window animated:YES];
    });
}


#pragma mark UIimage

// 添加渐变到 view 中
+ (void)addShadowAsInverseToView:(UIView *)view
{
    CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
    newShadow.frame  = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
    newShadow.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor];
    [view.layer addSublayer:newShadow];
}

// 添加模糊背景
+ (void)addBlurViewToView:(UIView *)view {
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = view.bounds;
    [view addSubview:effectview];
}
@end
